﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using BugAdmin.IService;
using BugAdmin.Model.Entity;
using BugAdmin.Api.Common;
using AutoMapper;
using BugAdmin.Model.Dto;
using SqlSugar;
using System.Collections.Generic;
using BugAdmin.Model.Other;
using Common.Tools;

namespace BugAdmin.Api.Controllers
{
    /// <summary>
    /// 用户相关控制器
    /// </summary>
    [Route("[controller]/[Action]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private IUserService _service;
        private IMapper _mapper;
        /// <summary>
        /// 用户控制器构造函数
        /// </summary>
        /// <param name="service"></param>
        /// <param name="mapper"></param>
        public UserController(IUserService service,IMapper mapper)
        {
            _service=service;
            _mapper=mapper;
        }
        /// <summary>
        /// 查询所有用户
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> QueryUsers()
        {
            ApiDataResult<List<User>> apiDataResult=new ApiDataResult<List<User>>();
            List<User> users=await _service.QueryAsync();
            if(users!=null)
            {
                apiDataResult.Success = true;
                apiDataResult.Message = "获取所有用户信息成功";
                apiDataResult.Data = users;
            }
            else
            {
                apiDataResult.Success = false;
                apiDataResult.Message = "获取所有用户信息失败";
            }
            return new JsonResult(apiDataResult);
        }
        /// <summary>
        /// 查询用户
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> QueryUserFirst()
        {
            ApiDataResult<UserInfoDto> apiDataResult = new ApiDataResult<UserInfoDto>();
            List<User> users = await _service.QueryAsync();
            if (users != null)
            {
                User user=users.First();
                UserInfoDto dto=_mapper.Map<User, UserInfoDto>(user);
                apiDataResult.Success = true;
                apiDataResult.Message = "获取用户信息成功";
                apiDataResult.Data = dto;
            }
            else
            {
                apiDataResult.Success = false;
                apiDataResult.Message = "获取用户信息失败";
            }
            return new JsonResult(apiDataResult);
        }
        /// <summary>
        /// 查询分页用户信息
        /// </summary>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("{page:int}/{size:int}")]
        public async Task<IActionResult> QueryUserAsync(int page, int size)
        {
            RefAsync<int> total = new RefAsync<int>();
            List<User> users= await _service.QueryAsync(page, size, total);

            ApiDataResult<PagingData<UserInfoDto>> apiDataResult = new ApiDataResult<PagingData<UserInfoDto>>();
            if (users != null)
            {
                PagingData<User> pagingData = new PagingData<User>()
                {
                    RecordCount=total,
                    PageIndex=page,
                    PageSize=size,
                    DataList=users
                };
                PagingData<UserInfoDto> Results = _mapper.Map<PagingData<User>, PagingData<UserInfoDto>>(pagingData);


                //List<UserInfoDto> usersDto = new List<UserInfoDto>();
                //foreach (User u in users)
                //{
                //    UserInfoDto dto=_mapper.Map<User,UserInfoDto>(u);
                //    usersDto.Add(dto);
                //}
                apiDataResult.Success = true;
                apiDataResult.Message = "获取分页用户信息成功";
                apiDataResult.Data = Results;
            }
            else
            {
                apiDataResult.Success = false;
                apiDataResult.Message = "获取分页用户信息失败";
            }
            return new JsonResult(apiDataResult);
        }
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> AddUserAsync([FromBody] AddUserDto dto)
        {
            User user=_mapper.Map<AddUserDto,User>(dto);
            user.Password = MD5Helper.MD5Encrypt(user.Password, "chongzi1");
            user.Status=dto.IsEnable?(int)StatusEnum.Normal:(int)StatusEnum.Frozen;
            user.UserType = (int)UserTypeEnum.GeneralUser;
            user.CreateTime=DateTime.Now;
            bool bRet= await _service.AddAsync(user);
            ApiDataResult<User> apiResult=new ApiDataResult<User>();
            apiResult.Success= bRet;
            if (bRet)
            {
                apiResult.Message = "添加用户成功";
                apiResult.Data = user;
            }
            else
            {
                apiResult.Message = "添加用户失败";
            }
            return new JsonResult(apiResult);
        }
    }
}
